00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _base_network_h_
00019 #define _base_network_h_
00020
00021 #include <fstream>
00022 #include <iomanip>
00023 #include <vector>
00024 #include <map>
00025 #include <boost/smart_ptr/shared_ptr.hpp>
00026 #include <boost/serialization/singleton.hpp>
00027 #include <boost/serialization/extended_type_info.hpp>
00028 #include <boost/serialization/shared_ptr.hpp>
00029 #include <boost/type_traits.hpp>
00030 #include <ga.h>
00031 #include "gridpack/parallel/distributed.hpp"
00032 #include "gridpack/parallel/index_hash.hpp"
00033 #include "gridpack/component/base_component.hpp"
00034 #include "gridpack/component/data_collection.hpp"
00035 #include "gridpack/partition/graph_partitioner.hpp"
00036 #include "gridpack/parallel/shuffler.hpp"
00037 #include "gridpack/parallel/ga_shuffler.hpp"
00038 #include "gridpack/timer/coarse_timer.hpp"
00039 #include "gridpack/utilities/exception.hpp"
00040 #include "gridpack/environment/environment.hpp"
00041
00042 namespace gridpack {
00043 namespace network {
00044
00045
00046
00047
00048
00049 template <class _bus>
00050 class BusData {
00051 public:
00052
00053
00054
00055
00056 BusData(void)
00057 : p_activeBus(true),
00058 p_originalBusIndex(-1),
00059 p_globalBusIndex(-1),
00060 p_branchNeighbors(),
00061 p_bus(new _bus),
00062 p_data(new gridpack::component::DataCollection),
00063 p_refFlag(false)
00064 {
00065 }
00066
00067
00068 BusData(const BusData& old)
00069 : p_activeBus(old.p_activeBus),
00070 p_originalBusIndex(old.p_originalBusIndex),
00071 p_globalBusIndex(old.p_globalBusIndex),
00072 p_branchNeighbors(old.p_branchNeighbors),
00073 p_bus(old.p_bus),
00074 p_data(old.p_data),
00075 p_refFlag(old.p_refFlag)
00076 {}
00077
00078
00079
00080
00081 ~BusData(void)
00082 {
00083 }
00084
00085
00086
00087
00088 BusData<_bus> & operator=(const BusData<_bus> & rhs)
00089 {
00090 if (this == &rhs) return *this;
00091 p_activeBus = rhs.p_activeBus;
00092 p_originalBusIndex = rhs.p_originalBusIndex;
00093 p_globalBusIndex = rhs.p_globalBusIndex;
00094 p_branchNeighbors = rhs.p_branchNeighbors;
00095 p_bus = rhs.p_bus;
00096 p_data = rhs.p_data;
00097 p_refFlag = rhs.p_refFlag;
00098 return *this;
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 bool p_activeBus;
00112 int p_originalBusIndex;
00113 int p_globalBusIndex;
00114 std::vector<int> p_branchNeighbors;
00115 boost::shared_ptr<_bus> p_bus;
00116 boost::shared_ptr<component::DataCollection> p_data;
00117 bool p_refFlag;
00118
00119 private:
00120
00121 friend class boost::serialization::access;
00122
00123
00124 template<class Archive> void serialize(Archive &ar, const unsigned int)
00125 {
00126 ar & p_activeBus
00127 & p_originalBusIndex
00128 & p_globalBusIndex
00129 & p_branchNeighbors
00130 & p_bus
00131 & p_data
00132 & p_refFlag;
00133 }
00134
00135 };
00136
00137
00138
00139
00140
00141 template <class _branch>
00142 class BranchData {
00143 public:
00144
00145
00146
00147
00148 BranchData(void)
00149 : p_activeBranch(true),
00150 p_globalBranchIndex(-1),
00151 p_originalBusIndex1(-1),
00152 p_originalBusIndex2(-1),
00153 p_globalBusIndex1(-1),
00154 p_globalBusIndex2(-1),
00155 p_localBusIndex1(-1),
00156 p_localBusIndex2(-1),
00157 p_branch(new _branch),
00158 p_data(new gridpack::component::DataCollection)
00159 {
00160 }
00161
00162
00163 BranchData(const BranchData& old)
00164 : p_activeBranch(old.p_activeBranch),
00165 p_globalBranchIndex(old.p_globalBranchIndex),
00166 p_originalBusIndex1(old.p_originalBusIndex1),
00167 p_originalBusIndex2(old.p_originalBusIndex2),
00168 p_globalBusIndex1(old.p_globalBusIndex1),
00169 p_globalBusIndex2(old.p_globalBusIndex2),
00170 p_localBusIndex1(old.p_localBusIndex1),
00171 p_localBusIndex2(old.p_localBusIndex2),
00172 p_branch(old.p_branch),
00173 p_data(old.p_data)
00174 {}
00175
00176
00177
00178
00179 ~BranchData(void)
00180 {
00181 }
00182
00183
00184
00185
00186 BranchData<_branch> & operator=(const BranchData<_branch> & rhs)
00187 {
00188 if (this == &rhs) return *this;
00189 p_activeBranch = rhs.p_activeBranch;
00190 p_globalBranchIndex = rhs.p_globalBranchIndex;
00191 p_originalBusIndex1 = rhs.p_originalBusIndex1;
00192 p_originalBusIndex2 = rhs.p_originalBusIndex2;
00193 p_globalBusIndex1 = rhs.p_globalBusIndex1;
00194 p_globalBusIndex2 = rhs.p_globalBusIndex2;
00195 p_localBusIndex1 = rhs.p_localBusIndex1;
00196 p_localBusIndex2 = rhs.p_localBusIndex2;
00197 p_branch = rhs.p_branch;
00198 p_data = rhs.p_data;
00199 return *this;
00200 }
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 bool p_activeBranch;
00218 int p_globalBranchIndex;
00219 int p_originalBusIndex1;
00220 int p_originalBusIndex2;
00221 int p_globalBusIndex1;
00222 int p_globalBusIndex2;
00223 int p_localBusIndex1;
00224 int p_localBusIndex2;
00225 boost::shared_ptr<_branch> p_branch;
00226 boost::shared_ptr<component::DataCollection> p_data;
00227
00228 private:
00229
00230 friend class boost::serialization::access;
00231
00232
00233 template<class Archive> void serialize(Archive &ar, const unsigned int)
00234 {
00235 ar & p_activeBranch
00236 & p_globalBranchIndex
00237 & p_originalBusIndex1
00238 & p_originalBusIndex2
00239 & p_globalBusIndex1
00240 & p_globalBusIndex2
00241 & p_localBusIndex1
00242 & p_localBusIndex2
00243 & p_branch
00244 & p_data;
00245 }
00246
00247 };
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263 template <class _bus, class _branch>
00264 class BaseNetwork
00265 : public parallel::Distributed
00266 {
00267
00268
00269 BOOST_STATIC_ASSERT((boost::is_base_of< component::BaseBusComponent, _bus >::value ));
00270
00271
00272 BOOST_STATIC_ASSERT((boost::is_base_of< component::BaseBranchComponent, _branch >::value ));
00273
00274 public:
00275
00276
00277
00278
00279 typedef _bus BusType;
00280 typedef _branch BranchType;
00281 typedef boost::shared_ptr<_bus> BusPtr;
00282 typedef boost::shared_ptr<_branch> BranchPtr;
00283
00284
00285
00286
00287 explicit BaseNetwork(const parallel::Communicator& comm)
00288 : parallel::Distributed(comm)
00289 {
00290 p_refBus = -1;
00291 p_busXCBufSize = 0;
00292 p_branchXCBufSize = 0;
00293 p_busXCBufType = 0;
00294 p_branchXCBufType = 0;
00295 p_busGASet = false;
00296 p_branchGASet = false;
00297 p_activeBusIndices = NULL;
00298 p_busSndBuf = NULL;
00299 p_inactiveBusIndices = NULL;
00300 p_busRcvBuf = NULL;
00301 p_activeBranchIndices = NULL;
00302 p_branchSndBuf = NULL;
00303 p_inactiveBranchIndices = NULL;
00304 p_branchRcvBuf = NULL;
00305 p_busXCBuffers = NULL;
00306 p_external_bus = false;
00307 p_branchXCBuffers = NULL;
00308 p_external_branch = false;
00309 p_allocatedBus = false;
00310 p_allocatedBranch = false;
00311 p_network_data.reset(new gridpack::component::DataCollection);
00312 }
00313
00314
00315
00316
00317 virtual ~BaseNetwork(void)
00318 {
00319 int i, size;
00320
00321 if (p_busXCBufSize != 0 && p_busXCBuffers != NULL) {
00322 int size = p_buses.size();
00323 int i;
00324 if (p_allocatedBus) {
00325 if (!p_external_bus) {
00326 for(i=0; i<size; i++) {
00327 delete [] static_cast<char*>(p_busXCBuffers[i]);
00328 }
00329 }
00330 p_allocatedBus = false;
00331 }
00332 delete [] p_busXCBuffers;
00333 }
00334 if (p_branchXCBufSize != 0 && p_branchXCBuffers != NULL) {
00335 int size = p_branches.size();
00336 int i;
00337 if (p_allocatedBranch) {
00338 if (!p_external_branch) {
00339 for(i=0; i<size; i++) {
00340 delete [] static_cast<char*>(p_branchXCBuffers[i]);
00341 }
00342 }
00343 p_allocatedBranch = false;
00344 }
00345 delete [] p_branchXCBuffers;
00346 }
00347 if (p_activeBusIndices) {
00348 for (i=0; i<p_numActiveBuses; i++) {
00349 delete p_activeBusIndices[i];
00350 }
00351 delete [] p_activeBusIndices;
00352 }
00353 if (p_busSndBuf) {
00354 delete [] ((char*)p_busSndBuf);
00355 }
00356 if (p_inactiveBusIndices) {
00357 for (i=0; i<p_numInactiveBuses; ++i) {
00358 delete p_inactiveBusIndices[i];
00359 }
00360 delete [] p_inactiveBusIndices;
00361 }
00362 if (p_busRcvBuf) {
00363 delete [] ((char*)p_busRcvBuf);
00364 }
00365 if (p_activeBranchIndices) {
00366 for (i=0; i<p_numActiveBranches; ++i) {
00367 delete p_activeBranchIndices[i];
00368 }
00369 delete [] p_activeBranchIndices;
00370 }
00371 if (p_branchSndBuf) {
00372 delete [] ((char*)p_branchSndBuf);
00373 }
00374 if (p_inactiveBranchIndices) {
00375 for (i=0; i<p_numInactiveBranches; ++i) {
00376 delete p_inactiveBranchIndices[i];
00377 }
00378 delete [] p_inactiveBranchIndices;
00379 }
00380 if (p_branchSndBuf) {
00381 delete [] ((char*)p_branchRcvBuf);
00382 }
00383 if (p_branchGASet) {
00384 GA_Destroy(p_branchGA);
00385 NGA_Deregister_type(p_branchXCBufType);
00386 }
00387 if (p_busGASet) {
00388 GA_Destroy(p_busGA);
00389 NGA_Deregister_type(p_busXCBufType);
00390 }
00391 }
00392
00393
00394
00395
00396
00397 void addBus(int idx)
00398 {
00399 BusDataPtr bus(new BusData<BusType>());
00400 bus->p_originalBusIndex = idx;
00401 bus->p_globalBusIndex = -1;
00402 p_buses.push_back(*bus);
00403 }
00404
00405
00406
00407
00408
00409
00410
00411 void addBranch(int idx1, int idx2)
00412 {
00413 BranchDataPtr branch(new BranchData<_branch>());
00414 branch->p_originalBusIndex1 = idx1;
00415 branch->p_originalBusIndex2 = idx2;
00416 branch->p_globalBusIndex1 = -1;
00417 branch->p_globalBusIndex2 = -1;
00418 p_branches.push_back(*branch);
00419 }
00420
00421
00422
00423
00424
00425 int numBuses(void)
00426 {
00427 return p_buses.size();
00428 }
00429
00430
00431
00432
00433
00434 int totalBuses(void)
00435 {
00436 int nBus = p_buses.size();
00437 int i, total;
00438 total = 0;
00439 for (i=0; i<nBus; i++) {
00440 if (p_buses[i].p_activeBus) total++;
00441 }
00442 int grp = this->communicator().getGroup();
00443 char plus[2];
00444 strcpy(plus,"+");
00445 GA_Pgroup_igop(grp,&total,1,plus);
00446 return total;
00447 }
00448
00449
00450
00451
00452
00453 int numBranches(void)
00454 {
00455 return p_branches.size();
00456 }
00457
00458
00459
00460
00461
00462 int totalBranches(void)
00463 {
00464 int nBranch = p_branches.size();
00465 int i, total;
00466 total = 0;
00467 for (i=0; i<nBranch; i++) {
00468 if (p_branches[i].p_activeBranch) total++;
00469 }
00470 int grp = this->communicator().getGroup();
00471 char plus[2];
00472 strcpy(plus,"+");
00473 GA_Pgroup_igop(grp,&total,1,plus);
00474 return total;
00475 }
00476
00477
00478
00479
00480
00481 void setReferenceBus(int idx)
00482 {
00483 p_refBus = idx;
00484 if (idx >= 0 && idx < p_buses.size()) {
00485 p_buses[idx].p_refFlag = true;
00486 } else {
00487 printf("Illegal bus index specified for reference bus idx: %d size: %d\n",
00488 idx,static_cast<int>(p_buses.size()));
00489 }
00490 }
00491
00492
00493
00494
00495
00496
00497 int getReferenceBus(void) const
00498 {
00499 return p_refBus;
00500 }
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510 bool setOriginalBusIndex(int idx, int o_idx)
00511 {
00512 if (idx < 0 || idx >= p_buses.size()) {
00513 return false;
00514 } else {
00515 p_buses[idx].p_originalBusIndex = o_idx;
00516 return true;
00517 }
00518 }
00519
00520
00521
00522
00523
00524
00525
00526 bool setGlobalBusIndex(int idx, int g_idx)
00527 {
00528 if (idx < 0 || idx >= p_buses.size()) {
00529 return false;
00530 } else {
00531 p_buses[idx].p_globalBusIndex = g_idx;
00532 return true;
00533 }
00534 }
00535
00536
00537
00538
00539
00540
00541
00542 bool setGlobalBranchIndex(int idx, int g_idx)
00543 {
00544 if (idx < 0 || idx >= p_branches.size()) {
00545 return false;
00546 } else {
00547 p_branches[idx].p_globalBranchIndex = g_idx;
00548 return true;
00549 }
00550 }
00551
00552
00553
00554
00555
00556
00557
00558 bool setOriginalBusIndex1(int idx, int b_idx)
00559 {
00560 if (idx < 0 || idx >= p_branches.size()) {
00561 return false;
00562 } else {
00563 p_branches[idx].p_originalBusIndex1 = b_idx;
00564 return true;
00565 }
00566 }
00567
00568
00569
00570
00571
00572
00573
00574 bool setOriginalBusIndex2(int idx, int b_idx)
00575 {
00576 if (idx < 0 || idx >= p_branches.size()) {
00577 return false;
00578 } else {
00579 p_branches[idx].p_originalBusIndex2 = b_idx;
00580 return true;
00581 }
00582 }
00583
00584
00585
00586
00587
00588
00589
00590 bool setGlobalBusIndex1(int idx, int b_idx)
00591 {
00592 if (idx < 0 || idx >= p_branches.size()) {
00593 return false;
00594 } else {
00595 p_branches[idx].p_globalBusIndex1 = b_idx;
00596 return true;
00597 }
00598 }
00599
00600
00601
00602
00603
00604
00605
00606 bool setGlobalBusIndex2(int idx, int b_idx)
00607 {
00608 if (idx < 0 || idx >= p_branches.size()) {
00609 return false;
00610 } else {
00611 p_branches[idx].p_globalBusIndex2 = b_idx;
00612 return true;
00613 }
00614 }
00615
00616
00617
00618
00619
00620
00621
00622 bool setLocalBusIndex1(int idx, int b_idx)
00623 {
00624 if (idx < 0 || idx >= p_branches.size()) {
00625 return false;
00626 } else {
00627 p_branches[idx].p_localBusIndex1 = b_idx;
00628 return true;
00629 }
00630 }
00631
00632
00633
00634
00635
00636
00637
00638 bool setLocalBusIndex2(int idx, int b_idx)
00639 {
00640 if (idx < 0 || idx >= p_branches.size()) {
00641 return false;
00642 } else {
00643 p_branches[idx].p_localBusIndex2 = b_idx;
00644 return true;
00645 }
00646 }
00647
00648
00649
00650
00651
00652
00653
00654 bool setActiveBus(int idx, bool flag)
00655 {
00656 if (idx < 0 || idx >= p_buses.size()) {
00657 return false;
00658 } else {
00659 p_buses[idx].p_activeBus = flag;
00660 return true;
00661 }
00662 }
00663
00664
00665
00666
00667
00668
00669
00670 bool setActiveBranch(int idx, bool flag)
00671 {
00672 if (idx < 0 || idx >= p_branches.size()) {
00673 return false;
00674 } else {
00675 p_branches[idx].p_activeBranch = flag;
00676 return true;
00677 }
00678 }
00679
00680
00681
00682
00683
00684
00685 bool clearBranchNeighbors(int idx)
00686 {
00687 if (idx < 0 || idx >= p_buses.size()) {
00688 return false;
00689 } else {
00690 p_buses[idx].p_branchNeighbors.clear();
00691 return true;
00692 }
00693 }
00694
00695
00696
00697
00698
00699
00700
00701 bool addBranchNeighbor(int idx, int br_idx)
00702 {
00703 if (idx < 0 || idx >= p_buses.size()) {
00704 return false;
00705 } else {
00706 p_buses[idx].p_branchNeighbors.push_back(br_idx);
00707 return true;
00708 }
00709 }
00710
00711
00712
00713
00714
00715
00716
00717
00718 bool getActiveBus(int idx)
00719 {
00720 if (idx >= 0 && idx < p_buses.size()) {
00721 return p_buses[idx].p_activeBus;
00722 } else {
00723 char buf[256];
00724 sprintf(buf,"BaseNetwork::getActiveBus: illegal index: %d size: %d\n",
00725 idx, static_cast<int>(p_buses.size()));
00726 printf("%s",buf);
00727 throw gridpack::Exception(buf);
00728 }
00729 return false;
00730 }
00731
00732
00733
00734
00735
00736
00737 int getOriginalBusIndex(int idx)
00738 {
00739 if (idx >= 0 && idx < p_buses.size()) {
00740 return p_buses[idx].p_originalBusIndex;
00741 } else {
00742 char buf[256];
00743 sprintf(buf,"BaseNetwork::getOriginalBusIndex: illegal index: %d size: %d\n",
00744 idx,static_cast<int>(p_buses.size()));
00745 printf("%s",buf);
00746 throw gridpack::Exception(buf);
00747 }
00748 return -1;
00749 }
00750
00751
00752
00753
00754
00755
00756 int getGlobalBusIndex(int idx)
00757 {
00758 if (idx >= 0 && idx < p_buses.size()) {
00759 return p_buses[idx].p_globalBusIndex;
00760 } else {
00761 char buf[256];
00762 sprintf(buf,"BaseNetwork::getGlobalBusIndex: illegal index: %d size: %d\n",
00763 idx,static_cast<int>(p_buses.size()));
00764 printf("%s",buf);
00765 throw gridpack::Exception(buf);
00766 }
00767 return -1;
00768 }
00769
00770
00771
00772
00773
00774
00775 BusPtr getBus(int idx)
00776 {
00777 if (idx<0 || idx >= p_buses.size()) {
00778 char buf[256];
00779 sprintf(buf,"BaseNetwork::getBus: illegal index: %d size: %d\n",
00780 idx,static_cast<int>(p_buses.size()));
00781 printf("%s",buf);
00782 throw gridpack::Exception(buf);
00783 } else {
00784 return p_buses[idx].p_bus;
00785 }
00786 boost::shared_ptr<_bus> null;
00787 return null;
00788 }
00789
00790
00791
00792
00793
00794
00795 bool getActiveBranch(int idx)
00796 {
00797 if (idx >= 0 && idx < p_branches.size()) {
00798 return p_branches[idx].p_activeBranch;
00799 } else {
00800 char buf[256];
00801 sprintf(buf,"BaseNetwork::getActiveBranch: illegal index: %d size: %d\n",
00802 idx, static_cast<int>(p_branches.size()));
00803 printf("%s",buf);
00804 throw gridpack::Exception(buf);
00805 }
00806 return false;
00807 }
00808
00809
00810
00811
00812
00813
00814 int getGlobalBranchIndex(int idx)
00815 {
00816 if (idx >= 0 && idx < p_branches.size()) {
00817 return p_branches[idx].p_globalBranchIndex;
00818 } else {
00819 char buf[256];
00820 sprintf(buf,"BaseNetwork::getGlobalBranchIndex: illegal index: %d size: %d\n",
00821 idx, static_cast<int>(p_branches.size()));
00822 printf("%s",buf);
00823 throw gridpack::Exception(buf);
00824 }
00825 return -1;
00826 }
00827
00828
00829
00830
00831
00832
00833 BranchPtr getBranch(int idx)
00834 {
00835 if (idx<0 || idx >= p_branches.size()) {
00836 char buf[256];
00837 sprintf(buf,"BaseNetwork::getBranch: illegal index: %d size: %d\n",
00838 idx, static_cast<int>(p_branches.size()));
00839 printf("%s",buf);
00840 throw gridpack::Exception(buf);
00841 } else {
00842 return p_branches[idx].p_branch;
00843 }
00844 boost::shared_ptr<_branch> null;
00845 return null;
00846 }
00847
00848
00849
00850
00851
00852
00853
00854 void getOriginalBranchEndpoints(int idx, int *idx1, int *idx2)
00855 {
00856 if (idx >= 0 && idx < p_branches.size()) {
00857 *idx1 = p_branches[idx].p_originalBusIndex1;
00858 *idx2 = p_branches[idx].p_originalBusIndex2;
00859 } else {
00860 char buf[256];
00861 sprintf(buf,"BaseNetwork::getGlobalBranchIndex: illegal index: %d size: %d\n",
00862 idx, static_cast<int>(p_branches.size()));
00863 printf("%s",buf);
00864 throw gridpack::Exception(buf);
00865 }
00866 }
00867
00868
00869
00870
00871
00872
00873
00874 boost::shared_ptr<component::DataCollection> getBusData(int idx)
00875 {
00876 if (idx<0 || idx >= p_buses.size()) {
00877 char buf[256];
00878 sprintf(buf,"BaseNetwork::getBusData: illegal index: %d size: %d\n",
00879 idx, static_cast<int>(p_buses.size()));
00880 printf("%s",buf);
00881 throw gridpack::Exception(buf);
00882 } else {
00883 return p_buses[idx].p_data;
00884 }
00885 boost::shared_ptr<component::DataCollection> null;
00886 return null;
00887 }
00888
00889
00890
00891
00892
00893
00894
00895 boost::shared_ptr<component::DataCollection> getBranchData(int idx)
00896 {
00897 if (idx<0 || idx >= p_branches.size()) {
00898 char buf[256];
00899 sprintf(buf,"BaseNetwork::getBranchData: illegal index: %d size: %d\n",
00900 idx, static_cast<int>(p_branches.size()));
00901 printf("%s",buf);
00902 throw gridpack::Exception(buf);
00903 } else {
00904 return p_branches[idx].p_data;
00905 }
00906 boost::shared_ptr<component::DataCollection> null;
00907 return null;
00908 }
00909
00910
00911
00912
00913
00914
00915 std::vector<int> getConnectedBranches(int idx) const
00916 {
00917 if (idx<0 || idx >= p_buses.size()) {
00918 char buf[256];
00919 sprintf(buf,"BaseNetwork::getConnectedBranches: illegal index: %d size: %d\n",
00920 idx, static_cast<int>(p_buses.size()));
00921 printf("%s",buf);
00922 throw gridpack::Exception(buf);
00923 } else {
00924 return p_buses[idx].p_branchNeighbors;
00925 }
00926 std::vector<int> null;
00927 return null;
00928 }
00929
00930
00931
00932
00933
00934
00935 std::vector<int> getConnectedBuses(int idx) const
00936 {
00937 if (idx<0 || idx >= p_buses.size()) {
00938 char buf[256];
00939 sprintf(buf,"BaseNetwork::getConnectedBuses: illegal index: %d size: %d\n",
00940 idx, static_cast<int>(p_buses.size()));
00941 printf("%s",buf);
00942 throw gridpack::Exception(buf);
00943 } else {
00944 std::vector<int> branches = p_buses[idx].p_branchNeighbors;
00945 int size = branches.size();
00946 std::vector<int> ret;
00947 int i, j;
00948 for (i=0; i<size; i++) {
00949 j = branches[i];
00950 if (p_branches[j].p_localBusIndex1 != idx) {
00951 ret.push_back(p_branches[j].p_localBusIndex1);
00952 } else {
00953 ret.push_back(p_branches[j].p_localBusIndex2);
00954 }
00955 }
00956 return ret;
00957 }
00958 std::vector<int> null;
00959 return null;
00960 }
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970 void getBranchEndpoints(int idx, int *bus1, int *bus2) const
00971 {
00972 if (idx<0 || idx >= p_branches.size()) {
00973 char buf[256];
00974 sprintf(buf,"BaseNetwork::getBranchEndpoints: illegal index: %d size: %d\n",
00975 idx, static_cast<int>(p_branches.size()));
00976 printf("%s",buf);
00977 throw gridpack::Exception(buf);
00978 } else {
00979 *bus1 = p_branches[idx].p_localBusIndex1;
00980 *bus2 = p_branches[idx].p_localBusIndex2;
00981 }
00982 }
00983
00984
00985 void assemble(void)
00986 {
00987 }
00988
00989
00990
00991
00992 void partition(void)
00993 {
00994 gridpack::utility::CoarseTimer *timer;
00995 timer = NULL;
00996
00997
00998 int t_total(0), t_part(0), t_bus_dist(0), t_branch_dist(0);
00999
01000 if (timer != NULL) {
01001 t_total = timer->createCategory("BaseNetwork<>::partition(): Total");
01002 t_part = timer->createCategory("BaseNetwork<>::partition(): Partitioner");
01003 t_bus_dist = timer->createCategory("BaseNetwork<>::partition(): Bus Distribution");
01004 t_branch_dist = timer->createCategory("BaseNetwork<>::partition(): Branch Distribution");
01005 }
01006
01007 if (timer != NULL) timer->start(t_total);
01008
01009 if (timer != NULL) timer->start(t_part);
01010
01011
01012 GraphPartitioner partitioner(this->communicator(),
01013 p_buses.size(), p_branches.size());
01014
01015 for (BusIterator bus = p_buses.begin();
01016 bus != p_buses.end(); ++bus) {
01017 partitioner.add_node(bus->p_globalBusIndex,bus->p_originalBusIndex);
01018 }
01019 for (BranchIterator branch = p_branches.begin();
01020 branch != p_branches.end(); ++branch) {
01021 partitioner.add_edge(branch->p_globalBranchIndex,
01022 branch->p_originalBusIndex1,
01023 branch->p_originalBusIndex2);
01024 }
01025 partitioner.partition();
01026
01027 int nbranch = p_branches.size();
01028 int idx;
01029 unsigned int index1, index2;
01030 for (idx=0; idx<nbranch; idx++) {
01031 partitioner.get_global_edge_ids(idx, &index1, &index2);
01032 p_branches[idx].p_globalBusIndex1 = static_cast<int>(index1);
01033 p_branches[idx].p_globalBusIndex2 = static_cast<int>(index2);
01034 }
01035
01036 if (timer != NULL) timer->stop(t_part);
01037
01038 int me(this->processor_rank());
01039 GraphPartitioner::IndexVector dest, gdest;
01040
01041 #if 1
01042 typedef parallel::Shuffler<BusData<BusType>, GraphPartitioner::Index> BusShufflerType;
01043 typedef parallel::Shuffler<BranchData<BranchType>, GraphPartitioner::Index> BranchShufflerType;
01044 #else
01045 typedef parallel::gaShuffler<BusData<BusType>, GraphPartitioner::Index> BusShufflerType;
01046 typedef parallel::gaShuffler<BranchData<BranchType>, GraphPartitioner::Index> BranchShufflerType;
01047 #endif
01048
01049 BusShufflerType bus_shuffler(this->communicator());
01050 BranchShufflerType branch_shuffler(this->communicator());
01051
01052
01053
01054
01055
01056 BusDataVector ghostbuses;
01057 GraphPartitioner::MultiIndexVector gnodedest;
01058 GraphPartitioner::IndexVector ghostbusdest;
01059 BusIterator bus(p_buses.begin());
01060 partitioner.ghost_node_destinations(gnodedest);
01061
01062 for (size_t i = 0; i < gnodedest.size(); ++i, ++bus) {
01063 for (GraphPartitioner::IndexVector::iterator d = gnodedest[i].begin();
01064 d != gnodedest[i].end(); ++d) {
01065 ghostbuses.push_back(*bus);
01066 ghostbusdest.push_back(*d);
01067 }
01068 }
01069
01070
01071
01072
01073 partitioner.edge_destinations(dest);
01074 partitioner.ghost_edge_destinations(gdest);
01075
01076 BranchDataVector ghostbranches;
01077 BranchIterator branch(p_branches.begin());
01078 GraphPartitioner::IndexVector ghostbranchdest;
01079
01080 for (size_t i = 0; i < dest.size(); ++i, ++branch) {
01081 if (dest[i] != gdest[i]) {
01082 ghostbranches.push_back(*branch);
01083 ghostbranches.back().p_activeBranch = false;
01084 ghostbranchdest.push_back(gdest[i]);
01085 }
01086 }
01087
01088
01089
01090
01091
01092
01093 if (timer != NULL) timer->start(t_bus_dist);
01094 partitioner.node_destinations(dest);
01095 bus_shuffler(p_buses, dest);
01096 if (timer != NULL) timer->stop(t_bus_dist);
01097
01098
01099
01100 if (timer != NULL) timer->start(t_branch_dist);
01101 partitioner.edge_destinations(dest);
01102 branch_shuffler(p_branches, dest);
01103 if (timer != NULL) timer->stop(t_branch_dist);
01104
01105
01106
01107
01108
01109
01110
01111 if (timer != NULL) timer->start(t_bus_dist);
01112 bus_shuffler(ghostbuses, ghostbusdest);
01113 for (bus = ghostbuses.begin(); bus != ghostbuses.end(); ++bus) {
01114 bus->p_activeBus = false;
01115 p_buses.push_back(*bus);
01116 }
01117 ghostbuses.clear();
01118 if (timer != NULL) timer->stop(t_bus_dist);
01119
01120 if (timer != NULL) timer->start(t_branch_dist);
01121 branch_shuffler(ghostbranches, ghostbranchdest);
01122 std::copy(ghostbranches.begin(), ghostbranches.end(),
01123 std::back_inserter(p_branches));
01124 ghostbranches.clear();
01125 if (timer != NULL) timer->stop(t_branch_dist);
01126
01127
01128
01129
01130
01131
01132 int active_buses(0), active_branches(0);
01133 {
01134 std::map<int, int> busindexes;
01135 int lidx(0);
01136 for (BusIterator b = p_buses.begin(); b != p_buses.end(); ++b, ++lidx) {
01137 clearBranchNeighbors(lidx);
01138 busindexes[b->p_globalBusIndex] = lidx;
01139 if (b->p_activeBus) active_buses += 1;
01140 }
01141
01142
01143 lidx = 0;
01144 for (BranchIterator b = p_branches.begin(); b != p_branches.end(); ++b, ++lidx) {
01145 int gbus, lbus1, lbus2;
01146 BusPtr bus1, bus2;
01147
01148
01149
01150 gbus = b->p_globalBusIndex1;
01151 lbus1 = busindexes[gbus];
01152 bus1 = p_buses[lbus1].p_bus;
01153
01154 gbus = b->p_globalBusIndex2;
01155 lbus2 = busindexes[gbus];
01156 bus2 = p_buses[lbus2].p_bus;
01157
01158 b->p_localBusIndex1 = lbus1;
01159 addBranchNeighbor(lbus1, lidx);
01160
01161 b->p_localBusIndex2 = lbus2;
01162 addBranchNeighbor(lbus2, lidx);
01163
01164
01165
01166 b->p_branch->setBus1(bus1);
01167 b->p_branch->setBus2(bus2);
01168
01169 gbus = b->p_globalBusIndex1;
01170 bus1->addBranch(b->p_branch);
01171 bus1->addBus(bus2);
01172 setGlobalBusIndex1(lidx,gbus);
01173 gbus = b->p_globalBusIndex2;
01174 bus2->addBranch(b->p_branch);
01175 bus2->addBus(bus1);
01176 setGlobalBusIndex2(lidx,gbus);
01177
01178 if (b->p_activeBranch) active_branches += 1;
01179 }
01180 }
01181 setMap();
01182
01183 std::cout << me << ": "
01184 << "I have "
01185 << p_buses.size() << " buses and "
01186 << p_branches.size() << " branches"
01187 << std::endl;
01188
01189 if (timer != NULL) timer->stop(t_total);
01190 }
01191
01192
01193
01194
01195
01196
01197
01198
01199 void clean(void)
01200 {
01201 std::map<int, int> buses;
01202 std::map<int, int> branches;
01203 std::map<int, int>::iterator p;
01204 int i, j;
01205
01206 freeXCBus();
01207 freeXCBranch();
01208 if (p_activeBusIndices) {
01209 for (i=0; i<p_numActiveBuses; ++i) {
01210 delete p_activeBusIndices[i];
01211 }
01212 delete [] p_activeBusIndices;
01213 p_activeBusIndices = NULL;
01214 }
01215 if (p_busSndBuf) {
01216 delete [] ((char*)p_busSndBuf);
01217 p_busSndBuf = NULL;
01218 }
01219 if (p_inactiveBusIndices) {
01220 for (i=0; i<p_numInactiveBuses; ++i) {
01221 delete p_inactiveBusIndices[i];
01222 }
01223 delete [] p_inactiveBusIndices;
01224 p_inactiveBusIndices = NULL;
01225 }
01226 if (p_busRcvBuf) {
01227 delete [] ((char*)p_busRcvBuf);
01228 p_busRcvBuf = NULL;
01229 }
01230 if (p_activeBranchIndices) {
01231 for (i=0; i<p_numActiveBranches; ++i) {
01232 delete p_activeBranchIndices[i];
01233 }
01234 delete [] p_activeBranchIndices;
01235 p_activeBranchIndices = NULL;
01236 }
01237 if (p_branchSndBuf) {
01238 delete [] ((char*)p_branchSndBuf);
01239 p_branchSndBuf = NULL;
01240 }
01241 if (p_inactiveBranchIndices) {
01242 for (i=0; i<p_numInactiveBranches; ++i) {
01243 delete p_inactiveBranchIndices[i];
01244 }
01245 delete [] p_inactiveBranchIndices;
01246 p_inactiveBranchIndices = NULL;
01247 }
01248 if (p_branchRcvBuf) {
01249 delete [] ((char*)p_branchRcvBuf);
01250 p_branchRcvBuf = NULL;
01251 }
01252
01253
01254 int size = p_branches.size();
01255 int new_id = 0;
01256 for (i=0; i<size; i++) {
01257 if (p_branches[i].p_activeBranch) {
01258 p_branches[new_id] = p_branches[i];
01259 branches.insert(std::pair<int, int>(i,new_id));
01260 new_id++;
01261 }
01262 }
01263
01264 size = size - new_id;
01265 for (i=0; i<size; i++) {
01266 p_branches.pop_back();
01267 }
01268
01269
01270 size = p_buses.size();
01271 new_id = 0;
01272 for (i=0; i<size; i++) {
01273 if (p_buses[i].p_activeBus) {
01274 p_buses[new_id] = p_buses[i];
01275 buses.insert(std::pair<int, int>(i,new_id));
01276 new_id++;
01277 }
01278 }
01279
01280 size = size - new_id;
01281 for (i=0; i<size; i++) {
01282 p_buses.pop_back();
01283 }
01284
01285
01286 size = p_branches.size();
01287 for (i=0; i<size; i++) {
01288 p = buses.find(p_branches[i].p_localBusIndex1);
01289 if (p != buses.end()) {
01290 p_branches[i].p_localBusIndex1 = p->second;
01291 } else {
01292 p_branches[i].p_localBusIndex1 = -1;
01293 }
01294 p = buses.find(p_branches[i].p_localBusIndex2);
01295 if (p != buses.end()) {
01296 p_branches[i].p_localBusIndex2 = p->second;
01297 } else {
01298 p_branches[i].p_localBusIndex2 = -1;
01299 }
01300 }
01301
01302 int jsize;
01303 size = p_buses.size();
01304 for (i=0; i<size; i++) {
01305 std::vector<int> neighbors = p_buses[i].p_branchNeighbors;
01306 jsize = neighbors.size();
01307 p_buses[i].p_branchNeighbors.clear();
01308 for (j=0; j<jsize; j++) {
01309 p = branches.find(neighbors[j]);
01310 if (p != branches.end()) {
01311 p_buses[i].p_branchNeighbors.push_back(p->second);
01312 }
01313 }
01314 }
01315 if (p_refBus != -1) {
01316 p_refBus = buses[p_refBus];
01317 }
01318 }
01319
01320
01321
01322
01323
01324
01325
01326
01327
01328 template <class _new_bus, class _new_branch> void clone(
01329 boost::shared_ptr<gridpack::network::BaseNetwork
01330 <_new_bus,_new_branch> > &new_network)
01331 {
01332 new_network->clear();
01333 int i, j, nsize, idx, jdx;
01334 int numBus = numBuses();
01335 int numBranch = numBranches();
01336
01337 for (i=0; i<numBus; i++) {
01338 idx = getOriginalBusIndex(i);
01339 new_network->addBus(idx);
01340 idx = getGlobalBusIndex(i);
01341 new_network->setGlobalBusIndex(i,idx);
01342 *(new_network->getBusData(i)) = *(getBusData(i));
01343 new_network->setActiveBus(i,getActiveBus(i));
01344
01345 std::vector<int> nghbrs;
01346 nghbrs = getConnectedBranches(i);
01347 nsize = nghbrs.size();
01348 for (j=0; j<nsize; j++) {
01349 new_network->addBranchNeighbor(i,nghbrs[j]);
01350 }
01351 }
01352
01353 if (getReferenceBus() != -1) {
01354 new_network->setReferenceBus(getReferenceBus());
01355 }
01356
01357 for (i=0; i<numBranch; i++) {
01358 getOriginalBranchEndpoints(i,&idx,&jdx);
01359 new_network->addBranch(idx,jdx);
01360 idx = getGlobalBranchIndex(i);
01361 new_network->setGlobalBranchIndex(i,idx);
01362 *(new_network->getBranchData(i)) = *(getBranchData(i));
01363 new_network->setActiveBranch(i,getActiveBranch(i));
01364
01365 getBranchEndpoints(i,&idx,&jdx);
01366 new_network->setLocalBusIndex1(i,idx);
01367 new_network->setLocalBusIndex2(i,jdx);
01368 j = getGlobalBusIndex(idx);
01369 new_network->setGlobalBusIndex1(i,j);
01370 j = getGlobalBusIndex(jdx);
01371 new_network->setGlobalBusIndex2(i,j);
01372 }
01373
01374 *(new_network->getNetworkData()) = *p_network_data;
01375
01376 new_network->setMap();
01377 }
01378
01379
01380
01381
01382
01383
01384
01385 void resetGlobalIndices(bool flag)
01386 {
01387 int i;
01388 int nprocs = communicator().size();
01389 int me = communicator().rank();
01390
01391 std::vector<int> idx_buf(nprocs);
01392 for (i=0; i<nprocs; i++) idx_buf[i] = 0;
01393
01394
01395 int localBuses = p_buses.size();
01396 int lcnt=0;
01397 for (i=0; i<localBuses; i++) {
01398 if (getActiveBus(i)) lcnt++;
01399 }
01400 idx_buf[me] = lcnt;
01401 communicator().sum(&idx_buf[0],nprocs);
01402
01403 int offset = 0;
01404 for (i=0; i<me; i++) offset += idx_buf[i];
01405 if (flag) {
01406 lcnt = 0;
01407 for (i=0; i<localBuses; i++) {
01408 if (getActiveBus(i)) {
01409 setGlobalBusIndex(i,lcnt+offset);
01410 lcnt++;
01411 }
01412 }
01413 }
01414
01415
01416
01417
01418
01419 std::vector<std::pair<int,int> > pairs;
01420 for (i=0; i<localBuses; i++) {
01421 if (getActiveBus(i)) {
01422 pairs.push_back(std::pair<int,int>(getOriginalBusIndex(i),
01423 getGlobalBusIndex(i)));
01424 }
01425 }
01426
01427 gridpack::hash_map::GlobalIndexHashMap hash_map(communicator());
01428 hash_map.addPairs(pairs);
01429
01430
01431 std::vector<int> keys;
01432 std::vector<int> values;
01433 for (i=0; i<localBuses; i++) {
01434 if (!getActiveBus(i)) {
01435 lcnt = getOriginalBusIndex(i);
01436 keys.push_back(lcnt);
01437 }
01438 }
01439 hash_map.getValues(keys,values);
01440
01441 std::map<int,int> lmap;
01442 std::map<int,int>::iterator lit;
01443 for (i=0; i<values.size(); i++) {
01444 if (lmap.find(keys[i]) == lmap.end()) {
01445 lmap.insert(std::pair<int,int>(keys[i],values[i]));
01446 }
01447 }
01448
01449 for (i=0; i<localBuses; i++) {
01450 if (!getActiveBus(i)) {
01451 lit = lmap.find(getOriginalBusIndex(i));
01452 if (lit != lmap.end()) {
01453 setGlobalBusIndex(i, lit->second);
01454 } else {
01455 printf("p[%d] No global index found for bus %d\n",
01456 me,getOriginalBusIndex(i));
01457 }
01458 }
01459 }
01460
01461 int localBranches = p_branches.size();
01462 int idx1, idx2;
01463 for (i=0; i<nprocs; i++) idx_buf[i] = 0;
01464
01465
01466
01467 lcnt=0;
01468 for (i=0; i<localBranches; i++) {
01469 if (getActiveBranch(i)) lcnt++;
01470 }
01471 for (i=0; i<nprocs; i++) idx_buf[i] = 0;
01472 idx_buf[me] = lcnt;
01473 communicator().sum(&idx_buf[0],nprocs);
01474
01475 offset = 0;
01476 for (i=0; i<me; i++) offset += idx_buf[i];
01477 if (flag) {
01478 lcnt = 0;
01479 for (i=0; i<localBranches; i++) {
01480 if (getActiveBranch(i)) {
01481 setGlobalBranchIndex(i,lcnt+offset);
01482 lcnt++;
01483 }
01484 }
01485 }
01486
01487
01488
01489
01490
01491 std::vector<std::pair<std::pair<int,int>,int> > branch_pairs;
01492 for (i=0; i<localBranches; i++) {
01493 if (getActiveBranch(i)) {
01494 lcnt++;
01495 getOriginalBranchEndpoints(i, &idx1, &idx2);
01496 branch_pairs.push_back(std::pair<std::pair<int,int>,int>(
01497 std::pair<int,int>(idx1,idx2),getGlobalBranchIndex(i)));
01498 }
01499 }
01500
01501 gridpack::hash_map::GlobalIndexHashMap branch_map(communicator());
01502 branch_map.addPairs(branch_pairs);
01503
01504
01505 std::vector<std::pair<int,int> > key_pairs;
01506 values.clear();
01507 for (i=0; i<localBranches; i++) {
01508 if (!getActiveBranch(i)) {
01509 getOriginalBranchEndpoints(i, &idx1, &idx2);
01510 key_pairs.push_back(std::pair<int,int>(idx1,idx2));
01511 }
01512 }
01513 branch_map.getValues(key_pairs,values);
01514
01515 std::map<std::pair<int,int>,int> lbmap;
01516 std::map<std::pair<int,int>,int>::iterator lbit;
01517
01518 for (i=0; i<key_pairs.size(); i++) {
01519 if (lbmap.find(key_pairs[i]) == lbmap.end()) {
01520 lbmap.insert(std::pair<std::pair<int,int>,int>(key_pairs[i],values[i]));
01521 }
01522 }
01523
01524 for (i=0; i<localBranches; i++) {
01525 if (!getActiveBranch(i)) {
01526 getOriginalBranchEndpoints(i, &idx1, &idx2);
01527 lbit = lbmap.find(std::pair<int,int>(idx1,idx2));
01528 if (lbit != lbmap.end()) {
01529 setGlobalBranchIndex(i, lbit->second);
01530 } else {
01531 printf("p[%d] Could not find branch pair (%d,%d)\n",me,idx1,idx2);
01532 }
01533 }
01534 }
01535 }
01536
01537
01538
01539
01540 void clear(void)
01541 {
01542 int i, size;
01543
01544 if (p_busXCBufSize != 0 && p_busXCBuffers != NULL) {
01545 int size = p_buses.size();
01546 int i;
01547 if (p_allocatedBus) {
01548 if (!p_external_bus) {
01549 for(i=0; i<size; i++) {
01550 delete [] static_cast<char*>(p_busXCBuffers[i]);
01551 }
01552 }
01553 p_allocatedBus = false;
01554 }
01555 delete [] p_busXCBuffers;
01556 }
01557 if (p_branchXCBufSize != 0 && p_branchXCBuffers != NULL) {
01558 int size = p_branches.size();
01559 int i;
01560 if (p_allocatedBranch) {
01561 if (!p_external_branch) {
01562 for(i=0; i<size; i++) {
01563 delete [] static_cast<char*>(p_branchXCBuffers[i]);
01564 }
01565 }
01566 p_allocatedBranch = false;
01567 }
01568 delete [] p_branchXCBuffers;
01569 }
01570 if (p_activeBusIndices) {
01571 for (i=0; i<p_numActiveBuses; i++) {
01572 delete p_activeBusIndices[i];
01573 }
01574 delete [] p_activeBusIndices;
01575 }
01576 if (p_busSndBuf) {
01577 delete [] ((char*)p_busSndBuf);
01578 }
01579 if (p_inactiveBusIndices) {
01580 for (i=0; i<p_numInactiveBuses; ++i) {
01581 delete p_inactiveBusIndices[i];
01582 }
01583 delete [] p_inactiveBusIndices;
01584 }
01585 if (p_busRcvBuf) {
01586 delete [] ((char*)p_busRcvBuf);
01587 }
01588 if (p_activeBranchIndices) {
01589 for (i=0; i<p_numActiveBranches; ++i) {
01590 delete p_activeBranchIndices[i];
01591 }
01592 delete [] p_activeBranchIndices;
01593 }
01594 if (p_branchSndBuf) {
01595 delete [] ((char*)p_branchSndBuf);
01596 }
01597 if (p_inactiveBranchIndices) {
01598 for (i=0; i<p_numInactiveBranches; ++i) {
01599 delete p_inactiveBranchIndices[i];
01600 }
01601 delete [] p_inactiveBranchIndices;
01602 }
01603 if (p_branchSndBuf) {
01604 delete [] ((char*)p_branchRcvBuf);
01605 }
01606 if (p_branchGASet) {
01607 GA_Destroy(p_branchGA);
01608 NGA_Deregister_type(p_branchXCBufType);
01609 }
01610 if (p_busGASet) {
01611 GA_Destroy(p_busGA);
01612 NGA_Deregister_type(p_busXCBufType);
01613 }
01614
01615 p_buses.clear();
01616 p_branches.clear();
01617
01618
01619 p_refBus = -1;
01620 p_busXCBufSize = 0;
01621 p_branchXCBufSize = 0;
01622 p_busXCBufType = 0;
01623 p_branchXCBufType = 0;
01624 p_busGASet = false;
01625 p_branchGASet = false;
01626 p_activeBusIndices = NULL;
01627 p_busSndBuf = NULL;
01628 p_inactiveBusIndices = NULL;
01629 p_busRcvBuf = NULL;
01630 p_activeBranchIndices = NULL;
01631 p_branchSndBuf = NULL;
01632 p_inactiveBranchIndices = NULL;
01633 p_branchRcvBuf = NULL;
01634 p_busXCBuffers = NULL;
01635 p_external_bus = false;
01636 p_branchXCBuffers = NULL;
01637 p_external_branch = false;
01638 p_allocatedBus = false;
01639 p_allocatedBranch = false;
01640 }
01641
01642
01643
01644
01645
01646 void allocXCBus(int size)
01647 {
01648 if (size < 0) {
01649 char buf[256];
01650 sprintf(buf,"BaseNetwork::allocXCBus: illegal buffer size: %d\n",
01651 size);
01652 printf("%s",buf);
01653 throw gridpack::Exception(buf);
01654 }
01655
01656 int nsize = p_buses.size();
01657 int i;
01658 if (p_busXCBufSize != 0 && p_busXCBuffers != NULL) {
01659 if (p_allocatedBus) {
01660 if (!p_external_bus) {
01661 for(i=0; i<nsize; i++) {
01662 delete [] static_cast<char*>(p_busXCBuffers[i]);
01663 }
01664 }
01665 p_allocatedBus = false;
01666 }
01667 delete [] p_busXCBuffers;
01668 p_busXCBufSize = 0;
01669 }
01670
01671 if (size > 0 && nsize > 0) {
01672 p_busXCBuffers = new void*[nsize];
01673 for (i=0; i<nsize; i++) {
01674 p_busXCBuffers[i] = static_cast<void*>(new char[size]);
01675 }
01676 p_busXCBufSize = size;
01677 p_allocatedBus = true;
01678 p_external_bus = false;
01679 }
01680 }
01681
01682
01683
01684
01685 void freeXCBus(void)
01686 {
01687
01688 if (p_busXCBufSize != 0 && p_busXCBuffers != NULL) {
01689 int i;
01690 int nsize = p_buses.size();
01691 if (p_allocatedBus) {
01692 if (!p_external_bus) {
01693 for(i=0; i<nsize; i++) {
01694 delete [] static_cast<char*>(p_busXCBuffers[i]);
01695 }
01696 }
01697 p_allocatedBus = false;
01698 }
01699 delete [] p_busXCBuffers;
01700 p_external_bus = false;
01701 p_busXCBufSize = 0;
01702 }
01703 }
01704
01705
01706
01707
01708
01709 void allocXCBusPointers(int size)
01710 {
01711
01712 int nsize = p_buses.size();
01713 int i;
01714 if (p_busXCBufSize != 0 && p_busXCBuffers != NULL) {
01715 if (p_allocatedBus) {
01716 if (!p_external_bus) {
01717 for(i=0; i<nsize; i++) {
01718 delete [] static_cast<char*>(p_busXCBuffers[i]);
01719 }
01720 }
01721 p_allocatedBus = false;
01722 }
01723 delete [] p_busXCBuffers;
01724 p_busXCBufSize = 0;
01725 }
01726
01727 if (size > 0) {
01728 p_busXCBuffers = new void*[nsize];
01729 p_busXCBufSize = size;
01730 }
01731 p_external_bus = true;
01732 }
01733
01734
01735
01736
01737
01738
01739 void setXCBusBuffer(int idx, void* ptr)
01740 {
01741 if (p_busXCBuffers != NULL && idx >= 0 && idx < p_buses.size()) {
01742 p_busXCBuffers[idx] = static_cast<char*>(ptr);
01743 } else {
01744 char buf[256];
01745 if (idx < 0 || idx >= p_buses.size()) {
01746 sprintf(buf,"BaseNetwork::setXCBusBuffer: illegal index: %d size: %ld\n",
01747 idx, p_buses.size());
01748 } else if (p_busXCBuffers == NULL) {
01749 sprintf(buf,"BaseNetwork::setXCBusBuffer: buffer already assigned\n");
01750 }
01751 printf("%s",buf);
01752 throw gridpack::Exception(buf);
01753 }
01754 }
01755
01756
01757
01758
01759
01760
01761 void* getXCBusBuffer(int idx)
01762 {
01763 if (idx < 0 || idx > p_buses.size()) {
01764 char buf[256];
01765 sprintf(buf,"BaseNetwork::getXCBusBuffer: illegal index: %d size: %ld\n",
01766 idx, p_buses.size());
01767 printf("%s",buf);
01768 throw gridpack::Exception(buf);
01769 } else {
01770 return static_cast<void*>(p_busXCBuffers[idx]);
01771 }
01772 return NULL;
01773 }
01774
01775
01776
01777
01778
01779 void allocXCBranch(int size)
01780 {
01781 if (size < 0) {
01782 char buf[256];
01783 sprintf(buf,"BaseNetwork::allocXCBranch: illegal size requested: %d\n",
01784 size);
01785 printf("%s",buf);
01786 throw gridpack::Exception(buf);
01787 }
01788
01789 int nsize = p_branches.size();
01790 int i;
01791 if (p_branchXCBufSize != 0 && p_branchXCBuffers != NULL) {
01792 if (p_allocatedBranch) {
01793 if (!p_external_branch) {
01794 for(i=0; i<nsize; i++) {
01795 delete [] static_cast<char*>(p_branchXCBuffers[i]);
01796 }
01797 }
01798 p_allocatedBranch = false;
01799 }
01800 delete [] p_branchXCBuffers;
01801 p_branchXCBufSize = 0;
01802 p_external_branch = true;
01803 }
01804
01805 if (size > 0 && nsize > 0) {
01806 p_branchXCBuffers = new void*[nsize];
01807 for (i=0; i<nsize; i++) {
01808 p_branchXCBuffers[i] = static_cast<void*>(new char[size]);
01809 }
01810 p_allocatedBranch = true;
01811 p_branchXCBufSize = size;
01812 }
01813 }
01814
01815
01816
01817
01818 void freeXCBranch(void)
01819 {
01820
01821 if (p_branchXCBufSize != 0 && p_branchXCBuffers != NULL) {
01822 int size = p_branches.size();
01823 int i;
01824 if (p_allocatedBranch) {
01825 if (!p_external_branch) {
01826 for(i=0; i<size; i++) {
01827 delete [] static_cast<char*>(p_branchXCBuffers[i]);
01828 }
01829 }
01830 p_allocatedBranch = false;
01831 }
01832 delete [] p_branchXCBuffers;
01833 p_branchXCBufSize = 0;
01834 p_external_branch = false;
01835 }
01836 }
01837
01838
01839
01840
01841
01842
01843 void* getXCBranchBuffer(int idx)
01844 {
01845 if (idx < 0 || idx > p_branches.size()) {
01846 char buf[256];
01847 sprintf(buf,"BaseNetwork::getXCBranchBuffer: illegal index: %d size: %ld\n",
01848 idx, p_branches.size());
01849 printf("%s",buf);
01850 throw gridpack::Exception(buf);
01851 } else {
01852 return static_cast<void*>(p_branchXCBuffers[idx]);
01853 }
01854 return NULL;
01855 }
01856
01857
01858
01859
01860
01861 void allocXCBranchPointers(int size)
01862 {
01863
01864 int nsize = p_branches.size();
01865 int i;
01866 if (p_branchXCBufSize != 0 && p_branchXCBuffers != NULL) {
01867 if (p_allocatedBranch) {
01868 if (!p_external_branch) {
01869 for(i=0; i<nsize; i++) {
01870 delete [] static_cast<char*>(p_branchXCBuffers[i]);
01871 }
01872 }
01873 p_allocatedBranch = false;
01874 }
01875 delete [] p_branchXCBuffers;
01876 p_branchXCBufSize = 0;
01877 }
01878
01879 if (size > 0) {
01880 p_branchXCBuffers = new void*[nsize];
01881 p_branchXCBufSize = size;
01882 }
01883 p_external_branch = true;
01884 }
01885
01886
01887
01888
01889
01890
01891 void setXCBranchBuffer(int idx, void* ptr)
01892 {
01893 if (p_branchXCBuffers != NULL && idx >= 0 && idx < p_branches.size()) {
01894 p_branchXCBuffers[idx] = static_cast<char*>(ptr);
01895 } else {
01896 char buf[256];
01897 if (idx < 0 || idx >= p_branches.size()) {
01898 sprintf(buf,"BaseNetwork::setXCBranchBuffer: illegal index: %d size: %ld\n",
01899 idx, p_branches.size());
01900 } else {
01901 sprintf(buf,"BaseNetwork::setXCBranchBuffer: buffer already assigned\n");
01902 }
01903 printf("%s",buf);
01904 throw gridpack::Exception(buf);
01905 }
01906 }
01907
01908
01909
01910
01911
01912 void initBusUpdate(void)
01913 {
01914 int i, size, numBuses;
01915 int grp = this->communicator().getGroup();
01916 GA_Pgroup_sync(grp);
01917
01918 if (p_busXCBufSize > 0) {
01919
01920 if (p_busGASet) {
01921 GA_Destroy(p_busGA);
01922 NGA_Deregister_type(p_busXCBufType);
01923 }
01924 if (p_activeBusIndices) {
01925 for (i=0; i<p_numActiveBuses; ++i) {
01926 delete p_activeBusIndices[i];
01927 }
01928 delete [] p_activeBusIndices;
01929 p_activeBusIndices = NULL;
01930 }
01931 if (p_busSndBuf) {
01932 delete [] ((char*)p_busSndBuf);
01933 p_busSndBuf = NULL;
01934 }
01935 if (p_inactiveBusIndices) {
01936 for (i=0; i<p_numInactiveBuses; ++i) {
01937 delete p_inactiveBusIndices[i];
01938 }
01939 delete [] p_inactiveBusIndices;
01940 p_inactiveBusIndices = NULL;
01941 }
01942 if (p_busRcvBuf) {
01943 delete [] ((char*)p_busRcvBuf);
01944 p_busRcvBuf = NULL;
01945 }
01946
01947 size = p_buses.size();
01948 numBuses = 0;
01949 int idx, icnt = 0, lcnt=0;
01950 for (i=0; i<size; i++) {
01951 if (getActiveBus(i)) {
01952 numBuses++;
01953 lcnt++;
01954 } else {
01955 icnt++;
01956 }
01957 }
01958
01959 int nprocs = GA_Pgroup_nnodes(grp);
01960 int me = GA_Pgroup_nodeid(grp);
01961 int *totBuses = new int[nprocs];
01962 int *distr = new int[nprocs];
01963 for (i=0; i<nprocs; i++) {
01964 if (me == i) {
01965 totBuses[i] = numBuses;
01966 } else {
01967 totBuses[i] = 0;
01968 }
01969 }
01970 char plus[2];
01971 strcpy(plus,"+");
01972 GA_Pgroup_igop(grp,totBuses,nprocs,plus);
01973 distr[0] = 0;
01974 p_busTotal = totBuses[0];
01975 for (i=1; i<nprocs; i++) {
01976 distr[i] = distr[i-1] + totBuses[i-1];
01977 p_busTotal += totBuses[i];
01978 }
01979 p_busGA = GA_Create_handle();
01980 int one = 1;
01981 p_busXCBufType = NGA_Register_type(p_busXCBufSize);
01982 GA_Set_data(p_busGA, one, &p_busTotal, p_busXCBufType);
01983 GA_Set_irreg_distr(p_busGA, distr, &nprocs);
01984 GA_Set_pgroup(p_busGA, grp);
01985 GA_Allocate(p_busGA);
01986 p_busGASet = true;
01987
01988 if (lcnt > 0) {
01989 p_activeBusIndices = new int*[lcnt];
01990 }
01991 for (i=0; i<lcnt; i++) {
01992 p_activeBusIndices[i] = new int;
01993 }
01994 p_numActiveBuses = lcnt;
01995 if (lcnt > 0) {
01996 p_busSndBuf = new char[lcnt*p_busXCBufSize];
01997 }
01998
01999 p_numInactiveBuses = icnt;
02000 if (icnt > 0) {
02001 p_inactiveBusIndices = new int*[icnt];
02002 }
02003 for (i=0; i<icnt; i++) {
02004 p_inactiveBusIndices[i] = new int;
02005 }
02006 if (icnt > 0) {
02007 p_busRcvBuf = new char[icnt*p_busXCBufSize];
02008 }
02009 lcnt = 0;
02010 icnt = 0;
02011 for (i=0; i<size; i++) {
02012 if (getActiveBus(i)) {
02013 *(p_activeBusIndices[lcnt]) = getGlobalBusIndex(i);
02014 idx = *(p_activeBusIndices[lcnt]);
02015 if (idx<0 || idx >= p_busTotal) {
02016 char buf[256];
02017 sprintf(buf,"BaseNetwork::initBusUpdate: illegal index: %d bus total: %d\n",
02018 idx, p_busTotal);
02019 printf("%s",buf);
02020 throw gridpack::Exception(buf);
02021 }
02022 lcnt++;
02023 } else {
02024 *(p_inactiveBusIndices[icnt]) = getGlobalBusIndex(i);
02025 idx = *(p_inactiveBusIndices[icnt]);
02026 if (idx<0 || idx >= p_busTotal) {
02027 char buf[256];
02028 sprintf(buf,"BaseNetwork::initBusUpdate: illegal index: %d bus total: %d\n",
02029 idx, p_busTotal);
02030 printf("%s",buf);
02031 throw gridpack::Exception(buf);
02032 }
02033 icnt++;
02034 }
02035 }
02036 delete [] totBuses;
02037 delete [] distr;
02038 }
02039 GA_Pgroup_sync(grp);
02040 }
02041
02042
02043
02044
02045
02046 void updateBuses(void)
02047 {
02048 int grp = this->communicator().getGroup();
02049
02050 GA_Pgroup_sync(grp);
02051 int i, j, xc_off, rs_off, icnt, nbus;
02052 char *rs_ptr, *xc_ptr;
02053 nbus = numBuses();
02054 icnt = 0;
02055 for (i=0; i<nbus; i++) {
02056 if (getActiveBus(i)) {
02057 xc_off = i*p_busXCBufSize;
02058 rs_off = icnt*p_busXCBufSize;
02059 xc_ptr = static_cast<char*>(p_busXCBuffers[i]);
02060 rs_ptr = ((char*)p_busSndBuf)+rs_off;
02061 memcpy(rs_ptr, xc_ptr, p_busXCBufSize);
02062
02063
02064
02065 icnt++;
02066 }
02067 }
02068
02069
02070 if (p_numActiveBuses > 0) {
02071 NGA_Scatter(p_busGA,p_busSndBuf,p_activeBusIndices,p_numActiveBuses);
02072 }
02073 GA_Pgroup_sync(grp);
02074 if (p_numInactiveBuses > 0) {
02075 NGA_Gather(p_busGA,p_busRcvBuf,p_inactiveBusIndices,p_numInactiveBuses);
02076 }
02077 GA_Pgroup_sync(grp);
02078
02079
02080 icnt = 0;
02081 for (i=0; i<nbus; i++) {
02082 if (!getActiveBus(i)) {
02083 xc_off = i*p_busXCBufSize;
02084 rs_off = icnt*p_busXCBufSize;
02085 xc_ptr = static_cast<char*>(p_busXCBuffers[i]);
02086 rs_ptr = ((char*)p_busRcvBuf)+rs_off;
02087 memcpy(xc_ptr, rs_ptr, p_busXCBufSize);
02088
02089
02090
02091 icnt++;
02092 }
02093 }
02094 GA_Pgroup_sync(grp);
02095 }
02096
02097
02098
02099
02100
02101 void initBranchUpdate(void)
02102 {
02103 int i, size, numBranches;
02104 int grp = this->communicator().getGroup();
02105 GA_Pgroup_sync(grp);
02106
02107 if (p_branchXCBufSize > 0) {
02108
02109 if (p_branchGASet) {
02110 GA_Destroy(p_branchGA);
02111 NGA_Deregister_type(p_branchXCBufType);
02112 }
02113 if (p_activeBranchIndices) {
02114 for (i=0; i<p_numActiveBranches; ++i) {
02115 delete p_activeBranchIndices[i];
02116 }
02117 delete [] p_activeBranchIndices;
02118 p_activeBranchIndices = NULL;
02119 if (p_branchSndBuf) {
02120 delete [] ((char*)p_branchSndBuf);
02121 p_branchSndBuf = NULL;
02122 }
02123 }
02124 if (p_inactiveBranchIndices) {
02125 for (i=0; i<p_numInactiveBranches; ++i) {
02126 delete p_inactiveBranchIndices[i];
02127 }
02128 delete [] p_inactiveBranchIndices;
02129 p_inactiveBranchIndices = NULL;
02130 if (p_branchRcvBuf) {
02131 delete [] ((char*)p_branchRcvBuf);
02132 p_branchRcvBuf = NULL;
02133 }
02134 }
02135
02136 size = p_branches.size();
02137 numBranches = 0;
02138 for (i=0; i<size; i++) {
02139 if (getActiveBranch(i)) {
02140 numBranches++;
02141 }
02142 }
02143
02144 int nprocs = GA_Pgroup_nnodes(grp);
02145 int me = GA_Pgroup_nodeid(grp);
02146 int *totBranches = new int(nprocs);
02147 int *distr = new int(nprocs);
02148 for (i=0; i<nprocs; i++) {
02149 if (me == i) {
02150 totBranches[i] = numBranches;
02151 } else {
02152 totBranches[i] = 0;
02153 }
02154 }
02155 char plus[2];
02156 strcpy(plus,"+");
02157 GA_Pgroup_igop(grp,totBranches,nprocs,plus);
02158 distr[0] = 0;
02159 p_branchTotal = totBranches[0];
02160 for (i=1; i<nprocs; i++) {
02161 distr[i] = distr[i-1] + totBranches[i-1];
02162 p_branchTotal += totBranches[i];
02163 }
02164 p_branchGA = GA_Create_handle();
02165 int one = 1;
02166 p_branchXCBufType = NGA_Register_type(p_branchXCBufSize);
02167 GA_Set_data(p_branchGA, one, &p_branchTotal, p_branchXCBufType);
02168 GA_Set_irreg_distr(p_branchGA, distr, &nprocs);
02169 GA_Set_pgroup(p_branchGA, grp);
02170 GA_Allocate(p_branchGA);
02171 p_branchGASet = true;
02172
02173 int idx, icnt = 0, lcnt=0;
02174 for (i=0; i<size; i++) {
02175 if (getActiveBranch(i)) {
02176 lcnt++;
02177 } else {
02178 icnt++;
02179 }
02180 }
02181 p_numActiveBranches = lcnt;
02182 p_activeBranchIndices = new int*[lcnt];
02183 p_branchSndBuf = new char[lcnt*p_branchXCBufSize];
02184 p_numInactiveBranches = icnt;
02185 p_inactiveBranchIndices = new int*[icnt];
02186 p_branchRcvBuf = new char[icnt*p_branchXCBufSize];
02187 lcnt = 0;
02188 icnt = 0;
02189 for (i=0; i<size; i++) {
02190 if (getActiveBranch(i)) {
02191 p_activeBranchIndices[lcnt] = new int(getGlobalBranchIndex(i));
02192 idx = *(p_activeBranchIndices[lcnt]);
02193 if (idx<0 || idx >= p_branchTotal) {
02194 char buf[256];
02195 sprintf(buf,"BaseNetwork::initBranchUpdate: illegal index: %d bus total: %d\n",
02196 idx, p_branchTotal);
02197 printf("%s",buf);
02198 throw gridpack::Exception(buf);
02199 }
02200 lcnt++;
02201 } else {
02202 p_inactiveBranchIndices[icnt] = new int(getGlobalBranchIndex(i));
02203 idx = *(p_inactiveBranchIndices[icnt]);
02204 if (idx<0 || idx >= p_branchTotal) {
02205 char buf[256];
02206 sprintf(buf,"BaseNetwork::initBranchUpdate: illegal index: %d bus total: %d\n",
02207 idx, p_branchTotal);
02208 printf("%s",buf);
02209 throw gridpack::Exception(buf);
02210 }
02211 icnt++;
02212 }
02213 }
02214 delete totBranches;
02215 delete distr;
02216 }
02217 GA_Pgroup_sync(grp);
02218 }
02219
02220
02221
02222
02223
02224 void updateBranches(void)
02225 {
02226
02227 int grp = this->communicator().getGroup();
02228 GA_Pgroup_sync(grp);
02229 int i, j, xc_off, rs_off, icnt, nbranch;
02230 char *rs_ptr, *xc_ptr;
02231 nbranch = numBranches();
02232 icnt = 0;
02233 for (i=0; i<nbranch; i++) {
02234 if (getActiveBranch(i)) {
02235 xc_off = i*p_branchXCBufSize;
02236 rs_off = icnt*p_branchXCBufSize;
02237 xc_ptr = static_cast<char*>(p_branchXCBuffers[i]);
02238 rs_ptr = ((char*)p_branchSndBuf)+rs_off;
02239 memcpy(rs_ptr, xc_ptr, p_branchXCBufSize);
02240
02241
02242
02243 icnt++;
02244 }
02245 }
02246
02247
02248 if (p_numActiveBranches > 0) {
02249 NGA_Scatter(p_branchGA,p_branchSndBuf,p_activeBranchIndices,p_numActiveBranches);
02250 }
02251 GA_Pgroup_sync(grp);
02252 if (p_numInactiveBranches > 0) {
02253 NGA_Gather(p_branchGA,p_branchRcvBuf,p_inactiveBranchIndices,p_numInactiveBranches);
02254 }
02255 GA_Pgroup_sync(grp);
02256
02257
02258 icnt = 0;
02259 for (i=0; i<nbranch; i++) {
02260 if (!getActiveBranch(i)) {
02261 xc_off = i*p_branchXCBufSize;
02262 rs_off = icnt*p_branchXCBufSize;
02263 xc_ptr = static_cast<char*>(p_branchXCBuffers[i]);
02264 rs_ptr = ((char*)p_branchRcvBuf)+rs_off;
02265 memcpy(xc_ptr, rs_ptr, p_branchXCBufSize);
02266
02267
02268
02269 icnt++;
02270 }
02271 }
02272 GA_Pgroup_sync(grp);
02273 }
02274
02275
02276
02277
02278
02279 void writeGraph(const std::string& outname)
02280 {
02281 static const bool internal_indexes(false);
02282 std::ofstream out;
02283 if (this->processor_rank() == 0) {
02284 out.open(outname.c_str(), std::ofstream::out | std::ofstream::trunc);
02285 out << "digraph {" << std::endl;
02286 out.close();
02287 }
02288
02289
02290 for (int p = 0; p < this->processor_size(); ++p) {
02291 if (p == this->processor_rank()) {
02292 out.open(outname.c_str(), std::ofstream::out | std::ofstream::app);
02293 out << "subgraph cluster_" << p << " {" << std::endl;
02294 out << "color=red" << std::endl;
02295 out << "label=" << p << ";" << std::endl;
02296 BusIterator bus;
02297 for (bus = p_buses.begin(); bus != p_buses.end(); ++bus) {
02298 if (bus->p_activeBus) {
02299 int bidx(internal_indexes ? bus->p_globalBusIndex : bus->p_originalBusIndex);
02300 out << " n" << bidx << "[label=" << bidx << "];" << std::endl;
02301 }
02302 }
02303 out << "}" << std::endl;
02304 out.close();
02305 }
02306 this->communicator().barrier();
02307 }
02308
02309
02310 for (int p = 0; p < this->processor_size(); ++p) {
02311 if (p == this->processor_rank()) {
02312 out.open(outname.c_str(), std::ofstream::out | std::ofstream::app);
02313 BranchIterator branch;
02314 for (branch = p_branches.begin(); branch != p_branches.end(); ++branch) {
02315 if (branch->p_activeBranch) {
02316 int bidx1(internal_indexes ? branch->p_globalBusIndex1 : branch->p_originalBusIndex1);
02317 int bidx2(internal_indexes ? branch->p_globalBusIndex2 : branch->p_originalBusIndex2);
02318 out << "n" << bidx1 << " -> "
02319 << "n" << bidx2 << ";"
02320 << std::endl;
02321 }
02322 }
02323 out.close();
02324 }
02325 this->communicator().barrier();
02326 }
02327
02328 this->communicator().barrier();
02329 if (this->processor_rank() == 0) {
02330 out.open(outname.c_str(), std::ofstream::out | std::ofstream::app);
02331 out << " } /* end */" << std::endl;
02332 out.close();
02333 }
02334
02335
02336 for (int p = 0; p < this->processor_size(); ++p) {
02337 if (p == this->processor_rank()) {
02338 out.open(outname.c_str(), std::ofstream::out | std::ofstream::app);
02339 out << "digraph \"" << p << "\" {" << std::endl;
02340 out << "label=\"Process " << p << "\";" << std::endl;
02341 out << "node [color=lightgrey];" << std::endl;
02342 BusIterator bus;
02343 for (bus = p_buses.begin(); bus != p_buses.end(); ++bus) {
02344 std::string color("black");
02345 std::string style("\"\"");
02346 if (!bus->p_activeBus) {
02347 color = "red";
02348 style = "dotted";
02349 }
02350 int bidx(internal_indexes ? bus->p_globalBusIndex : bus->p_originalBusIndex);
02351 out << " n" << bidx
02352 << " ["
02353 << "label=" << bidx << ", "
02354 << "color=" << color << ", "
02355 << "style=" << style
02356 << "];" << std::endl;
02357 }
02358 BranchIterator branch;
02359 for (branch = p_branches.begin(); branch != p_branches.end(); ++branch) {
02360 std::string color("black");
02361 std::string style("solid");
02362 if (!branch->p_activeBranch) {
02363 color = "red";
02364 style = "dotted";
02365 }
02366 int bidx1(internal_indexes ? branch->p_globalBusIndex1 : branch->p_originalBusIndex1);
02367 int bidx2(internal_indexes ? branch->p_globalBusIndex2 : branch->p_originalBusIndex2);
02368 out << "n" << bidx1 << " -> "
02369 << "n" << bidx2 << " "
02370 << "["
02371 << "color=" << color << ", "
02372 << "style=" << style
02373 << "]" << ";"
02374 << std::endl;
02375 }
02376 out << "} /* end process " << p << " */" << std::endl;
02377 out.close();
02378 }
02379 this->communicator().barrier();
02380 }
02381 }
02382
02383
02384
02385
02386 void setMap(void)
02387 {
02388 int nbus = numBuses();
02389 int nbranch = numBranches();
02390 int i,idx,idx1,idx2;
02391 p_busMap.clear();
02392 for (i=0; i<nbus; i++) {
02393 idx = getOriginalBusIndex(i);
02394 p_busMap.insert(std::pair<int,int>(idx,i));
02395 }
02396 p_branchMap.clear();
02397 for (i=0; i<nbranch; i++) {
02398 getOriginalBranchEndpoints(i, &idx1, &idx2);
02399 p_branchMap.insert(std::pair<std::pair<int,int>,int>(std::pair<int,int>(idx1,idx2),i));
02400 }
02401 }
02402
02403
02404
02405
02406
02407
02408
02409 std::vector<int> getLocalBusIndices(int idx) {
02410 std::multimap<int,int>::iterator it;
02411 it = p_busMap.find(idx);
02412 std::vector<int> ret;
02413 if (it != p_busMap.end()) {
02414 while (it != p_busMap.upper_bound(idx)) {
02415 ret.push_back(it->second);
02416 it++;
02417 }
02418 }
02419 return ret;
02420 }
02421
02422
02423
02424
02425
02426
02427
02428 std::vector<int> getLocalBranchIndices(int idx1, int idx2) {
02429 std::multimap<std::pair<int,int>,int>::iterator it;
02430 std::pair<int,int> pair = std::pair<int,int>(idx1,idx2);
02431 it = p_branchMap.find(pair);
02432 std::vector<int> ret;
02433 if (it != p_branchMap.end()) {
02434 while (it != p_branchMap.upper_bound(pair)) {
02435 ret.push_back(it->second);
02436 it++;
02437 }
02438 }
02439 return ret;
02440 }
02441
02442
02443
02444
02445
02446
02447
02448 boost::shared_ptr<gridpack::component::DataCollection> getNetworkData()
02449 {
02450 return p_network_data;
02451 }
02452
02453
02454
02455
02456
02457
02458 void broadcastNetworkData(int idx)
02459 {
02460 boost::mpi::communicator comm = this->communicator().getCommunicator();
02461 boost::mpi::broadcast(comm, p_network_data, idx);
02462 }
02463 protected:
02464
02465
02466
02467
02468 BaseNetwork(const BaseNetwork& old)
02469 {
02470 }
02471
02472 private:
02473
02474
02475
02476
02477 typedef boost::shared_ptr< BusData<BusType> > BusDataPtr;
02478 typedef std::vector< BusData<BusType> > BusDataVector;
02479 typedef typename BusDataVector::iterator BusIterator;
02480 typedef boost::shared_ptr< BranchData<BranchType> > BranchDataPtr;
02481 typedef std::vector< BranchData<BranchType> > BranchDataVector;
02482 typedef typename BranchDataVector::iterator BranchIterator;
02483
02484
02485
02486
02487 BusDataVector p_buses;
02488
02489
02490
02491
02492 BranchDataVector p_branches;
02493
02494
02495
02496
02497 int p_refBus;
02498
02499
02500
02501
02502 int p_busXCBufSize;
02503 void **p_busXCBuffers;
02504 bool p_allocatedBus;
02505 bool p_external_bus;
02506
02507
02508
02509
02510 int p_branchXCBufSize;
02511 void **p_branchXCBuffers;
02512 bool p_allocatedBranch;
02513 bool p_external_branch;
02514
02515
02516
02517
02518
02519
02520 int p_busGA;
02521 bool p_busGASet;
02522 int p_busXCBufType;
02523 int p_busTotal;
02524 int **p_inactiveBusIndices;
02525 int p_numInactiveBuses;
02526 int **p_activeBusIndices;
02527 int p_numActiveBuses;
02528 void *p_busSndBuf;
02529 void *p_busRcvBuf;
02530
02531
02532
02533
02534
02535
02536 int p_branchGA;
02537 bool p_branchGASet;
02538 int p_branchXCBufType;
02539 int p_branchTotal;
02540 int **p_inactiveBranchIndices;
02541 int p_numInactiveBranches;
02542 int **p_activeBranchIndices;
02543 int p_numActiveBranches;
02544 void *p_branchSndBuf;
02545 void *p_branchRcvBuf;
02546
02547
02548
02549
02550 std::multimap<int,int> p_busMap;
02551 std::multimap<std::pair<int,int>,int> p_branchMap;
02552
02553
02554
02555
02556 boost::shared_ptr<gridpack::component::DataCollection> p_network_data;
02557 };
02558 }
02559 }
02560
02561 #endif